package com.woniuxy.controller;

import com.woniuxy.dto.MyDto;
import com.woniuxy.pojo.MerchantAuth;
import com.woniuxy.pojo.MerchantUsers;
import com.woniuxy.pojo.User;
import com.woniuxy.service.UserService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@Controller
public class MerchantUsersController {
    @Autowired
    private UserService userService;

    /**
     * 增加用户
     * @param
     * @return
     */
    @PostMapping("/addpuser.do")
    @ApiOperation(value = "注册用户",httpMethod = "POST",
            protocols = "HTTP",produces = "application/json",
            response = MyDto.class,notes = "注册用户")
    @ResponseBody
    public MyDto add(@ApiParam(required = true,name = "m_phoneNum",value = "手机号")String m_phoneNum ,
                     @ApiParam(required = true,name = "m_password",value = "密码")String m_password,
                     @ApiParam(required = true,name = "mName",value = "名称")String mName){
        MerchantUsers users =new MerchantUsers();
         users.setmPhonenum(m_phoneNum);
         users.setmPassword(m_password);
         users.setmName(mName);
        MyDto myDto=new MyDto();
        users.setmState("-1");
        try {
            userService.addpUser(users);
            myDto.setCode(200);
            myDto.setMsg("增加成功");
        }catch (Exception e){
            myDto.setCode(-200);
            myDto.setMsg("增加失败");
        }
        return myDto;
    }


    /**
     * 提交审核订单
     * @param users
     * @return
     */
    @ApiOperation(value = "提交审核订单",httpMethod = "POST",
            protocols = "HTTP",produces = "application/json",
            response = MyDto.class,notes = "提交审核订单")
    @PostMapping("/register.do")
    @ResponseBody
    public MyDto register(MerchantAuth users){
        System.out.println(users.getmName());
        Subject subject= SecurityUtils.getSubject();
        Session session=subject.getSession();
        User user=(User) session.getAttribute("USER_SESSION");
        MerchantUsers iter=(MerchantUsers) session.getAttribute("msg");
        users.setmAddress(iter.getmAddress());
        users.setLnglat(iter.getLnglat());
        MyDto myDto=new MyDto();
        users.setmId(user.getId());

        try {
            userService.register(users);
            myDto.setCode(200);
            myDto.setMsg("认证中");
        }catch (Exception e){
            e.printStackTrace();
            myDto.setCode(-200);
            myDto.setMsg("信息填写有问题");
        }
        return  myDto;
    }
    @GetMapping("/findmerchantuser")
    @ApiOperation(value = "查找用户",httpMethod = "GET",
            protocols = "HTTP",produces = "application/json",
            response = MyDto.class,notes = "查找用户")
    @ResponseBody
    public MyDto find(){
        Subject subject= SecurityUtils.getSubject();
        Session session=subject.getSession();
        User user=(User) session.getAttribute("USER_SESSION");
        MyDto myDto=new MyDto(200,"");
        try{
            MerchantUsers merchantUsers =userService.findPuserByid(user.getId());
            myDto.setCode(200);
            myDto.setData(merchantUsers);
        }catch (Exception e){
            e.printStackTrace();
            myDto.setCode(-200);

        }

        return myDto;
    }
    @PostMapping("/lnglat.do")
    @ApiOperation(value = "经纬度地址",httpMethod = "POST",
            protocols = "HTTP",produces = "application/json",
            response = MyDto.class,notes = "经纬度地址")
    @ResponseBody
    public  MyDto save(MerchantUsers users){
        MyDto myDto=new MyDto();
      try {
          Subject subject= SecurityUtils.getSubject();
          Session session=subject.getSession();
          session.setAttribute("msg",users);
          myDto.setCode(200);
          myDto.setMsg("地址设置成功");
      }catch (Exception e){
          e.printStackTrace();
          myDto.setCode(-200);  myDto.setMsg("地址设置失败");
      }
     return myDto;
    }
}
